#!/bin/bash
#
DOCROOT=`grep -Po '^chdir = \K.*' /etc/php-fpm.d/www.conf 2>/dev/null`
if [ -z ${DOCROOT} ];then
	DOCROOT="/usr/local/emhttp"
fi

logger "Mounting 'Auto Mount' Remote Shares..." -t"unassigned.devices"
# Wait until network is ready
SECONDS=0
while [ $SECONDS -le 120 ]; do
	gateway=$(route -n | awk '/^0.0.0.0/ {print $2; exit}')
	if [ -n "$gateway" ] && [ $(ping -q -n -c 1 -W 1 $gateway | awk -F, '/received/ {print $2}' | awk '{print $1}') -eq 1 ]; then
		break
	fi
	sleep 1.0
done
if [ $SECONDS -le 120 ]; then
	${DOCROOT}/plugins/unassigned.devices/scripts/get_ud_stats ping;
	${DOCROOT}/plugins/unassigned.devices/scripts/rc.unassigned mount autoshares &
else
	logger "Cannot 'Auto Mount' Remote Shares.  Network not available!" -t"unassigned.devices"
fi
